package com.lzx.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzx.shop.dao.CategoryDao;
import com.lzx.shop.dao.ProductDao;
import com.lzx.shop.dataobject.CategoryDO;
import com.lzx.shop.dataobject.ProductDO;
import com.lzx.shop.dataobject.StoreDO;
import com.lzx.shop.dataobject.UserDO;
import com.lzx.shop.params.ProductParam;
import com.lzx.shop.params.StoreParam;
import com.lzx.shop.service.ProductService;
import com.lzx.shop.service.StoreService;
import com.lzx.shop.utils.UUIDUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 刘正星
 * @version 1.0.0
 * @ClassName ProductServiceImpl.java
 * @Description TODO
 * @createTime 2021年03月21日 13:16:00
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductDao productDao;

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private StoreService storeService;

    @Override
    public boolean save(ProductDO productDO, HttpSession session) {

        UserDO userDO = (UserDO) session.getAttribute("userInfo");
        StoreParam storeParam = new StoreParam();
        storeParam.setUserId(userDO.getId());
        StoreDO storeDO = storeService.query(storeParam);

        productDO.setGmtModified(LocalDateTime.now());
        if (StringUtils.isBlank(productDO.getId())){
            productDO.setId(UUIDUtil.getUUID());
            //商品销量初始为0
            productDO.setPerchaseNum(0);
            //商品状态初始为0
            productDO.setStatus(0);
            productDO.setStoreId(storeDO.getId());
            productDO.setGmtCreated(LocalDateTime.now());

        return productDao.insert(productDO)>0;
        }
        return productDao.updateById(productDO)>0;
    }


    @Override
    public List<ProductDO> findAll() {
        List<ProductDO> productDOS = productDao.selectList(null);
        List<String> categoryIds = productDOS.stream().map(ProductDO::getCategoryId).distinct().collect(Collectors.toList());

        Map<String, CategoryDO> categoryDOMap = categoryDao.selectBatchIds(categoryIds).stream().collect(Collectors.toMap(CategoryDO::getId, (categoryDO) -> categoryDO));
        productDOS.forEach(productDO -> {
            productDO.setCategoryDO(categoryDOMap.get(productDO.getCategoryId()));
        });
        return productDOS;
    }

    @Override
    public ProductDO findById(String id) {
        return productDao.selectById(id);
    }

    @Override
    public boolean update(ProductDO productDO) {

        productDO.setGmtModified(LocalDateTime.now());
        return productDao.updateById(productDO)>0;
    }

    @Override
    public boolean DeleteById(String id) {
        return productDao.deleteById(id)>0;
    }

    @Override
    public Integer count() {
        return productDao.selectCount(null);
    }

    @Override
    public IPage<ProductDO> selectPage(Page<ProductDO> page, Wrapper<ProductDO> wrapper) {
        IPage<ProductDO> productDOIPage = productDao.selectPage(page,wrapper);
        return productDOIPage;
    }

    @Override
    public List<ProductDO> search(ProductParam param) {
        QueryWrapper<ProductDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(wrapper->wrapper.like("name",param.getName()).or()
        .like("store_id",param.getStoreId()).or()
                .like("category_id",param.getCategoryId()).or()
        .like("status",param.getStatus()));

        return productDao.selectList(queryWrapper);
    }

    @Override
    public Boolean upOrDown(String id) {
        ProductDO productDO = productDao.selectById(id);
        if (productDO.getStatus()==0){
            productDO.setStatus(1);
            return  productDao.updateById(productDO)>0;
        }
        productDO.setStatus(0);
        return productDao.updateById(productDO)>0;
    }

    @Override
    public List<ProductDO> Query(Wrapper<ProductDO> wrapper) {
        return productDao.selectList(wrapper);
    }

    @Override
    public ProductDO save(ProductDO product) {
        return productDao.insert(product) > 0 ? product:null;
    }
}
